﻿using System;
using System.IO;
using RestSharp;//依赖版本106.15.0 https://www.nuget.org/packages/RestSharp/106.15.0
using Newtonsoft.Json; //https://www.nuget.org/packages/Newtonsoft.Json
using Api.Bll;
using Api.Model;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;


namespace Api.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    [EnableCors("_myAllowSpecificOrigins")]
    public class ChatRobot
    {

        const string API_KEY = "dLUTElWshR5pEFWJQpfr9nuD";
        const string SECRET_KEY = "ARimRvJn7cbsz43B95pKesXTLFg0P3a0";

        [HttpPost("chat")]
        public string chat(string problem)
        {
            //ERNIE-4.0-8K

            //var client = new RestClient($"https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/completions_pro?access_token={GetAccessToken()}");
            //client.Timeout = -1;
            //var request = new RestRequest(Method.POST);
            //request.AddHeader("Content-Type", "application/json");
            //string s1 = @"{""messages"":[{""role"":""user"",""content"":""";
            //string s2 = @"""}],""temperature"":0.95,""top_p"":0.8,""penalty_score"":1,""enable_system_memory"":false,""disable_search"":false,""enable_citation"":false}";
            ////var body = @"{ ""messages"":[{""role"":""user"",""content"":""请介绍一下中国最有名的非遗""}],""temperature"":0.95,""top_p"":0.8,""penalty_score"":1,""enable_system_memory"":false,""disable_search"":false,""enable_citation"":false}";
            //var body = s1 + problem + s2;
            //request.AddParameter("application/json", body, ParameterType.RequestBody);
            //IRestResponse response = client.Execute(request);
            //Console.WriteLine(response.Content);
            //return response.Content;

            //ERNIE Speed（免费）

            var client = new RestClient($"https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/ernie-speed-128k?access_token={GetAccessToken()}");
            client.Timeout = -1;
            var request = new RestRequest(Method.POST);
            request.AddHeader("Content-Type", "application/json");
            string s1= @"{""messages"":[{""role"":""user"",""content"":""";
            string s2 = @"""}]}";
            var body = s1 + problem + s2;
            //var body = @"{ ""messages"":[{""role"":""user"",""content"":""请介绍一下皮影戏""}]}";
            request.AddParameter("application/json", body, ParameterType.RequestBody);
            IRestResponse response = client.Execute(request);
            Console.WriteLine(response.Content);
            return response.Content;

        }


        /**
        * 使用 AK，SK 生成鉴权签名（Access Token）
        * @return 鉴权签名信息（Access Token）
        */
        static string GetAccessToken()
        {
            //var client = new RestClient($"https://aip.baidubce.com/oauth/2.0/token");
            //client.Timeout = -1;
            //var request = new RestRequest(Method.POST);
            //request.AddParameter("grant_type", "client_credentials");
            //request.AddParameter("client_id", API_KEY);
            //request.AddParameter("client_secret", SECRET_KEY);
            //IRestResponse response = client.Execute(request);
            //Console.WriteLine(response.Content);
            //var result = JsonConvert.DeserializeObject<dynamic>(response.Content);
            //return result.access_token.ToString();
            return "24.350f5b4cc449c13c0c9d4369d9b24d79.2592000.1734082504.282335-116222175";
        }

        //返回json
 /*
        [HttpPost("chat")]
        public IActionResult chat(string problem)
        {
            var client = new RestClient($"https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/ernie-speed-128k?access_token={GetAccessToken()}");
            client.Timeout = -1;
            var request = new RestRequest(Method.POST);
            request.AddHeader("Content-Type", "application/json");
            string s1 = @"{""messages"":[{""role"":""user"",""content"":""";
            string s2 = @"""}]}";
            var body = s1 + problem + s2;
            request.AddParameter("application/json", body, ParameterType.RequestBody);
            IRestResponse response = client.Execute(request);

            // 返回JSON数据
            return new JsonResult(response.Content);

        }
 */

    }
}




